/* 
 *  Copyright (c) 2011—2013 Panguso.com. All rights reserved. 
 *  File Name:  BusinessPatternHelper.java                                      
 *  File Contents Outline :  业务模板持久化类
 *  File  Create Date:   2012年7月30日
 *  File Version#: Revision#: #1                  
 *  File  Latest Modify Date : 2013年1月16日
 *  File Author :  bachuan@panguso.com
 */
package storage.dbhelper;

import cn.edu.thu.log.web.service.PropertiesReaderServices;
import cn.edu.thu.log.web.service.impl.PropertiesReaderServicesImpl;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.cassandra.thrift.ColumnOrSuperColumn;
import org.apache.cassandra.thrift.KeySlice;
import storage.dbobject.DbBusinessPattern;
import storage.util.StorageManager;

public class BusinessPatternHelper {

    /**
     * 将BusinessPattern保存到LaUDMS数据库中
     *
     * @param BusinessPattern 要存储的模型对象
     */
    public void saveBusinessPattern(DbBusinessPattern BusinessPattern) {
            PropertiesReaderServices x1 = PropertiesReaderServicesImpl.instance();
        StorageManager sm = new StorageManager(x1.getProperties("db_keySpace"), x1.getProperties("db_columnFamily_businessPattern"));
    //    StorageManager sm = new StorageManager(StorageParameter.db_keySpace, StorageParameter.db_columnFamily_businessPattern);
//        sm.open();
        sm.setRowKey(BusinessPattern.getRowKey());
        sm.put(DbBusinessPattern.cl_name, BusinessPattern.getName());
        sm.put(DbBusinessPattern.cl_updateTime, BusinessPattern.getUpdateTime());
        sm.put(DbBusinessPattern.cl_timeStamp, BusinessPattern.getTimeStamp());
        sm.put(DbBusinessPattern.cl_mainCaseID, BusinessPattern.getMainCaseID());
        sm.put(DbBusinessPattern.cl_product, BusinessPattern.getProduct());
        ArrayList<String> activityList = BusinessPattern.getActivityList();
        for (int i = 0; i < activityList.size(); i++) {
            sm.put(DbBusinessPattern.cl_activity + "_" + activityList.get(i), activityList.get(i));
        }
        ArrayList<String> caseIDList = BusinessPattern.getCaseIDList();
        for (int i = 0; i < caseIDList.size(); i++) {
            sm.put(DbBusinessPattern.cl_caseID + "_" + caseIDList.get(i), caseIDList.get(i));
        }
          ArrayList<String> attributeList = BusinessPattern.getAttributeList();
        for (int i = 0; i < attributeList.size(); i++) {
            sm.put(DbBusinessPattern.cl_attribute + "_" + attributeList.get(i), attributeList.get(i));
        }
        sm.put(DbBusinessPattern.cl_classifyTag,BusinessPattern.getClassifytag());
//        sm.close();

    }

    /**
     * 将一系列logHead保存到LaUDMS数据库中
     *
     * @param logHeads 要存储的模型对象列表
     */
    public void saveBusinessPatterns(List<DbBusinessPattern> BusinessPatterns) {
                  PropertiesReaderServices x1 = PropertiesReaderServicesImpl.instance();
        StorageManager sm = new StorageManager(x1.getProperties("db_keySpace"), x1.getProperties("db_columnFamily_businessPattern"));
//        sm.open();
        for (int j = 0; j < BusinessPatterns.size(); j++) {
            DbBusinessPattern BusinessPattern = BusinessPatterns.get(j);
            sm.setRowKey(BusinessPattern.getRowKey());
            sm.put(DbBusinessPattern.cl_name, BusinessPattern.getName());
            sm.put(DbBusinessPattern.cl_updateTime, BusinessPattern.getUpdateTime());
            sm.put(DbBusinessPattern.cl_timeStamp, BusinessPattern.getTimeStamp());
            sm.put(DbBusinessPattern.cl_mainCaseID, BusinessPattern.getMainCaseID());
            sm.put(DbBusinessPattern.cl_product, BusinessPattern.getProduct());
            ArrayList<String> activityList = BusinessPattern.getActivityList();
            for (int i = 0; i < activityList.size(); i++) {
                sm.put(DbBusinessPattern.cl_activity + "_" + activityList.get(i), activityList.get(i));
            }
            ArrayList<String> caseIDList = BusinessPattern.getCaseIDList();
            for (int i = 0; i < caseIDList.size(); i++) {
                sm.put(DbBusinessPattern.cl_caseID + "_" + caseIDList.get(i), caseIDList.get(i));
            }
             ArrayList<String> attributeList = BusinessPattern.getAttributeList();
        for (int i = 0; i < attributeList.size(); i++) {
            sm.put(DbBusinessPattern.cl_attribute + "_" + attributeList.get(i), attributeList.get(i));
        }
         sm.put(DbBusinessPattern.cl_classifyTag,BusinessPattern.getClassifytag());
//            sm.close();
        }
    }

    public List<DbBusinessPattern> getBusinessPatterns(final String columnName, final String value) {
        List<DbBusinessPattern> result = new LinkedList<DbBusinessPattern>();
                PropertiesReaderServices x1 = PropertiesReaderServicesImpl.instance();
        StorageManager sm = new StorageManager(x1.getProperties("db_keySpace"), x1.getProperties("db_columnFamily_businessPattern"));
//        sm.open();
        List<KeySlice> crs = sm.get(columnName, value);
        try {
            for (KeySlice ks : crs) {
                String rowKey = new String(ks.getKey());
                String name = null, product = null, mainCaseID = null, updateTime = null, timeStamp = null,classifytag=null;
                ArrayList<String> activityList = new ArrayList();
                ArrayList<String> caseIDList = new ArrayList();
                ArrayList<String> attributeList = new ArrayList();
                
                for (ColumnOrSuperColumn column : ks.getColumns()) {
                    String columnsName = new String(column.getColumn().getName());
                    String columnsValue = new String(column.getColumn().getValue());
                    if (columnsName.equals(DbBusinessPattern.cl_name)) {
                        name = columnsValue;
                    } else if (columnsName.equals(DbBusinessPattern.cl_product)) {
                        product = columnsValue;
                    } else if (columnsName.equals(DbBusinessPattern.cl_mainCaseID)) {
                        mainCaseID = columnsValue;
                    } else if (columnsName.equals(DbBusinessPattern.cl_timeStamp)) {
                        timeStamp = columnsValue;
                    } else if (columnsName.equals(DbBusinessPattern.cl_updateTime)) {
                        updateTime = columnsValue;
                    } else if (columnsName.startsWith(DbBusinessPattern.cl_activity)) {
                        activityList.add(columnsValue);
                    } else if (columnsName.startsWith(DbBusinessPattern.cl_caseID)) {
                        caseIDList.add(columnsValue);
                    }
                    else if (columnsName.startsWith(DbBusinessPattern.cl_attribute)) {
                        attributeList.add(columnsValue);
                    }
                    else if(columnsName.equals(DbBusinessPattern.cl_classifyTag)){
                        classifytag=columnsValue;
                    }
                }
                DbBusinessPattern pBusinessPattern = new DbBusinessPattern(rowKey, name, product, mainCaseID, timeStamp, updateTime, caseIDList, activityList,attributeList,classifytag);
                result.add(pBusinessPattern);
            }
        } catch (Exception ex) {
            Logger.getLogger(DbBusinessPattern.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
//            sm.close();
        }
        return result;
    }

    public ArrayList<String> getAllBusinessPatternName() {
            PropertiesReaderServices x1 = PropertiesReaderServicesImpl.instance();
        StorageManager sm = new StorageManager(x1.getProperties("db_keySpace"), x1.getProperties("db_columnFamily_businessPattern"));
//        sm.open();
        List<KeySlice> crs = sm.getAll();
        ArrayList<String> BusinessPatternNumList = new ArrayList<String>();
        try {
            for (KeySlice ks : crs) {
                String BusinessPatternName = null;
                for (ColumnOrSuperColumn column : ks.getColumns()) {
                    String columnsName = new String(column.getColumn().getName());
                    String columnsValue = new String(column.getColumn().getValue());
                    if (columnsName.equals(DbBusinessPattern.cl_name)) {
                        BusinessPatternName = columnsValue;
                    }
                }
                if (!BusinessPatternNumList.contains(BusinessPatternName)) {
                    BusinessPatternNumList.add(BusinessPatternName);
                }
            }
        } catch (Exception ex) {
            Logger.getLogger(DbBusinessPattern.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
//            sm.close();
        }
        return BusinessPatternNumList;
    }

    public void deleteBusinessPattern(final String rowKey) {
                PropertiesReaderServices x1 = PropertiesReaderServicesImpl.instance();
        StorageManager sm = new StorageManager(x1.getProperties("db_keySpace"), x1.getProperties("db_columnFamily_businessPattern"));
//        sm.open();
        sm.delete(rowKey);
//        sm.close();
    }

    /**
     * 为产品添加日志体字段
     *
     * @param BusinessPattern 产品
     * @param logHead 日志体字段
     */
    public void addActivity(DbBusinessPattern BusinessPattern, String activity) {
           PropertiesReaderServices x1 = PropertiesReaderServicesImpl.instance();
        StorageManager sm = new StorageManager(x1.getProperties("db_keySpace"), x1.getProperties("db_columnFamily_businessPattern"));
//        sm.open();
        sm.setRowKey(BusinessPattern.getRowKey());
        sm.put(DbBusinessPattern.cl_activity + "_" + activity, activity);
//        sm.close();

    }

    public void addCaseID(DbBusinessPattern BusinessPattern, String caseID) {
             PropertiesReaderServices x1 = PropertiesReaderServicesImpl.instance();
        StorageManager sm = new StorageManager(x1.getProperties("db_keySpace"), x1.getProperties("db_columnFamily_businessPattern"));
//        sm.open();
        sm.setRowKey(BusinessPattern.getRowKey());
        sm.put(DbBusinessPattern.cl_caseID + "_" + caseID, caseID);
//        sm.close();

    }
//    /**
//     *
//     */
//    public void deleteLogHead(DbBusinessPattern BusinessPattern, String logHead) {
//        StorageManager sm = new StorageManager(StorageParameter.db_keySpace, StorageParameter.db_columnFamily_businessPattern);
////        sm.open();
//        sm.setRowKey(BusinessPattern.getRowKey());
//        // sm.put(DbBusinessPattern.cl_logBody + "_" + logBody, logBody);
//        sm.delete(DbBusinessPattern.cl_logHead + "_" + logHead, logHead);
////        sm.close();
//
//    }
//
//    public void deleteLogBody(String BusinessPatternName, String logBodyname) {
//        throw new UnsupportedOperationException("Not yet implemented");
//    }
    public static void main(String [] args){
        BusinessPatternHelper bph = new BusinessPatternHelper();
        List<DbBusinessPattern> p1 = bph.getBusinessPatterns(DbBusinessPattern.cl_name, "image_block_contentType_pageType");
        
    }
}
